<template>
  <div class="login_container">
    <div class="login_box">
      <div style="position:absolute;top:50px; left:50px">
        <el-form ref="loginFormRef" :rules="loginFormrules" :model="loginForm" label-width="80px">
          <el-form-item label="賬號" prop="username">
            <el-input v-model="loginForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密碼" prop="password">
            <el-input type="password" v-model="loginForm.password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('loginFormRef')">登錄</el-button>
            <el-button type="success" @click="resetForm('loginFormRef')">重置</el-button>
          </el-form-item>

        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return{
      loginForm:{
        username:'admin',
        password:'123456'
      },
      loginFormrules:{
        username:[
             { required: true, message: '请输入賬號', trigger: 'blur' },
             { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }

        ],
        password:[
            { required: true, message: '请输入活动密碼', trigger: 'blur' },
            // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }

        ]
      }
    }
  },

  methods: {
      submitForm(formName) {
        this.$refs[formName].validate(async(valid) => {
          if (valid) {
           const res= await this.$http.post('login',this.loginForm);
           console.log(res.data);
           if(res.data.meta.status !==200){
             this.$message.error(
               '登錄失敗' )
           }else{
               this.$message({

               message:'登錄成功',
               type:"success"})

               window.sessionStorage.setItem('token',res.data.data.token)

              this.$router.push('/home')
           }
          } else {
            return false;
          }
        });
      },
     resetForm(formName) {
        this.$refs[formName].resetFields();
      }
  },
};
</script>

<style scoped>
.login_container {
  width: 100%;
  height: 100%;
  background-color: beige;
  /* display: flex;
  justify-self:center;
  align-items: center; */
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: bisque;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
</style>
